<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <j-select-inFo
                :msg="dataId"
                :disabled="disableSubmit"
                v-decorator="['jianKangXinXiId', validatorRules.jianKangXinXiId]"
                :trigger-change="true"
               @addChange='addValue'
              />
           
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用药名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                :disabled="disableSubmit"
                v-decorator="['yongYaoMingCheng', validatorRules.yongYaoMingCheng]"
                dict="gkak_yp_yaopin,shang_pin_ming_cheng,id"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="用药方法" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['yongYaoFangFa', validatorRules.yongYaoFangFa]"
                :trigger-change="true"
                dictCode="gkak_yyff"
                placeholder="请选择用药方法"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用药时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择用药时间"
                v-decorator="['yongYaoShiJian', validatorRules.yongYaoShiJian]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="药品用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['yaoPinYongLiang', validatorRules.yaoPinYongLiang]"
                placeholder="请输入药品用量"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="用药说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['yongYaoShuoMing', validatorRules.yongYaoShuoMing]"
                placeholder="请输入用药说明"
              ></a-input>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="用药依从性" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['yongYaoYiCongXing', validatorRules.yongYaoYiCongXing]"
                :trigger-change="true"
                dictCode="gkak_yyycx"
                placeholder="请选择用药依从性"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="不良反应" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['buLiangFanYing', validatorRules.buLiangFanYing]"
                :trigger-change="true"
                dictCode="gkak_blfy"
                placeholder="请选择不良反应"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="12">
            <a-form-item label="不良反应说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['buLiangFanYingShuoMing', validatorRules.buLiangFanYingShuoMing]"
                placeholder="请输入不良反应说明"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['beiZhu', validatorRules.beiZhu]"
                rows="4"
                placeholder="请输入备注"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/exactJDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JSelectInFo from '@/components/jeecgbiz/JSelectInFo'

export default {
  name: 'GkakDaYongYaoJiLuModal',
  components: {
    JDate,
    JDictSelectTag,
    JSearchSelectTag,
    JSelectInFo
  },
  data() {
    return {
      form: this.$form.createForm(this),
      disableSubmit: false,
      dataId: {
        id: '',
        name: ''
      },
      addXingMing:'',
      title: '操作',
      width: 1000,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        jianKangXinXiId: { rules: [{ required: true, message: '请选择姓名!' }] },
        yongYaoMingCheng: { rules: [{ required: true, message: '请输入用药名称!' }] },
        yongYaoFangFa: { rules: [{ required: true, message: '请输入用药方法!' }] },
        yongYaoShiJian: { rules: [{ required: true, message: '请输入用药时间!' }] },
        yaoPinYongLiang: { rules: [] },
        yongYaoShuoMing: { rules: [] },
        yongYaoYiCongXing: { rules: [] },
        buLiangFanYing: { rules: [] },
        buLiangFanYingShuoMing: { rules: [] },
        beiZhu: { rules: [] }
      },
      url: {
        add: '/gkakDaYongYaoJiLu/add',
        edit: '/gkakDaYongYaoJiLu/edit'
      }
    }
  },

  created() {
    this.$nextTick(() => {
      if (this.$route.query.yongHuBiaoShi) {
        if (localStorage.getItem('jiangkangInfo')) {
          let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
          console.log(row)
          this.form.setFieldsValue({
            jianKangXinXiId: row.id,
            xingMing: row.xingMing
          })
        }
      }
    })
  },
  methods: {
    addValue(val){
      this.addXingMing=val.xingMing
    },
    inFoId(record) {
        this.dataId = {
          id: record.jianKangXinXiId,
          name: record.xingMing
        }
     
    },
    add() {
      this.edit({})
    },

    edit(record) {
      
       if (this.$route.query.jianKangXinXiId) {
        if (localStorage.getItem('jiangkangInfo')) {
          let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
          console.log(row)
          this.dataId = {
            id: row.id,
            name:row.xingMing
          }
        }
      } else {
        this.inFoId(record)
      }
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'xingMing',
            'yongYaoMingCheng',
            'yongYaoFangFa',
            'yongYaoShiJian',
            'yaoPinYongLiang',
            'yongYaoShuoMing',
            'yongYaoYiCongXing',
            'buLiangFanYing',
            'buLiangFanYingShuoMing',
            'beiZhu',
            'jianKangXinXiId'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          if (this.$route.query.yongHuBiaoShi) {
            if (localStorage.getItem('jiangkangInfo')) {
              let row = JSON.parse(localStorage.getItem('jiangkangInfo'))
              console.log(row)
              values.yongHuBiaoShi = row.id
              values.xingMing = row.xingMing
            }
          }else{
            values.xingMing= this.addXingMing
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
              that.confirmLoading = false

          httpAction(httpurl, formData, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'xingMing',
          'yongYaoMingCheng',
          'yongYaoFangFa',
          'yongYaoShiJian',
          'yaoPinYongLiang',
          'yongYaoShuoMing',
          'yongYaoYiCongXing',
          'buLiangFanYing',
          'buLiangFanYingShuoMing',
          'beiZhu',
          'jianKangXinXiId'
        )
      )
    }
  }
}
</script>
